package com.songtai.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.songtai.dao.NewsDao;
import com.songtai.po.News;
import com.songtai.util.Pager;

/**
 * @author 赵中杨
 * @email zhaozhongyang717@gmail.com
 * @date 2010 2010-4-19 下午05:04:32
 */
public class NewsDaoImpl implements NewsDao {
	private SessionFactory sessionFactory;

	/**
	 * 增加新闻
	 */
	@Override
	public void addNews(News news) {
		this.sessionFactory.getCurrentSession().persist(news);
	}

	/**
	 * 获取指定节点下的新闻列表
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Pager<News> getNewsByNodeid(Long nodeid, int currentPage, int pageSize) {
		Session sesion = this.sessionFactory.getCurrentSession();
		int totalCount = sesion.createQuery("from News n where n.nid=:nodeid").setLong("nodeid", nodeid).list().size();
		int pageCount = (totalCount % pageSize == 0) ? totalCount / pageSize : (totalCount / pageSize) + 1;
		List<News> list = sesion.createQuery("from News n where n.nid=:nodeid order by n.recordTime desc").setLong("nodeid", nodeid).setFirstResult((currentPage - 1) * pageSize).setMaxResults(pageSize).list();
		Pager<News> pager = new Pager<News>(list, currentPage, totalCount, pageCount);
		return pager;
	}

	/**
	 * 删除新闻
	 */
	@Override
	public boolean deleteNews(String newsid) {
		int deleted = this.sessionFactory.getCurrentSession().createQuery("delete from News n where n.id in(" + newsid + ")").executeUpdate();
		if (deleted > 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 改变新闻状态
	 */
	@Override
	public int changeNewsStatus(String status1, String status2, String newsid) {
		return sessionFactory.getCurrentSession().createQuery("update News n set n.status=:status1 where n.status=:status2 and n.id in(" + newsid + ") ").setString("status1", status1).setString("status2", status2).executeUpdate();
	}

	/**
	 * 获取新闻
	 */
	@Override
	public News getNewsById(Long newsid) {
		return (News) sessionFactory.getCurrentSession().get(News.class, newsid);
	}

	/**
	 * 更新新闻
	 */
	@Override
	public void updateNews(News news) {
		sessionFactory.getCurrentSession().update(news);
	}

	/**
	 * 获取最新新闻
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<News> getRecentNews(int pageSize,Long nodeid) {
		return sessionFactory.getCurrentSession().createQuery("from News n where n.status=1 and nid=:nodeid order by n.recordTime desc").setLong("nodeid",nodeid).setMaxResults(pageSize).list();
	}

	/**
	 * 获取所有新闻
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Pager<News> getAllNews(int currentPage, int pageSize) {
		Session sesion = this.sessionFactory.getCurrentSession();
		int totalCount = sesion.createQuery("from News n where n.status=1 ").list().size();
		int pageCount = (totalCount % pageSize == 0) ? totalCount / pageSize : (totalCount / pageSize) + 1;
		List<News> list = sesion.createQuery("from News n  where n.status=1  order by n.recordTime desc").setFirstResult((currentPage - 1) * pageSize).setMaxResults(pageSize).list();
		Pager<News> pager = new Pager<News>(list, currentPage, totalCount, pageCount);
		return pager;
	}

	/**
	 * 搜索新闻
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Pager<News> searchNews(int currentPage, int pageSize, String keyword) {
		Session sesion = this.sessionFactory.getCurrentSession();
		int totalCount = sesion.createQuery("from News n where n.status=1 and n.title like ? ").setString(0, "%" + keyword + "%").list().size();
		int pageCount = (totalCount % pageSize == 0) ? totalCount / pageSize : (totalCount / pageSize) + 1;
		List<News> list = sesion.createQuery("from News n where n.status=1  and n.title like ? order by n.recordTime desc ").setString(0, "%" + keyword + "%").setFirstResult((currentPage - 1) * pageSize).setMaxResults(pageSize).list();
		Pager<News> pager = new Pager<News>(list, currentPage, totalCount, pageCount);
		return pager;
	}

	/**
	 * @return the sessionFactory
	 */
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	/**
	 * @param sessionFactory
	 *            the sessionFactory to set
	 */
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

}
